//
//  DDCommentGalleryView.m
//  Dingdong
//
//  Created by 程强 on 16/12/8.
//  Copyright © 2016年 程强. All rights reserved.
//

#import "DDCommentGalleryView.h"

@interface DDCommentGalleryView()<UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) UITableView *horTable;
@end

@implementation DDCommentGalleryView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        
        CGRect rect = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height); //tableview旋转之后的位置
        self.horTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.bounds.size.height, self.bounds.size.width) style:UITableViewStylePlain];
        _horTable.delegate = self;
        _horTable.dataSource = self;
        [self addSubview:_horTable];
        _horTable.transform = CGAffineTransformMakeRotation(-M_PI / 2);
        _horTable.center = CGPointMake(rect.origin.x + rect.size.width/2, rect.origin.y + rect.size.height/2);
        _horTable.showsVerticalScrollIndicator = NO;
        _horTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return self;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.galleryAry.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 75.0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *indentifier = @"OptionalCell";
    
    DDCommentGalleryCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifier];
    
    if (!cell) {
        cell = [[DDCommentGalleryCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.transform =CGAffineTransformMakeRotation(M_PI / 2);
    [cell.icon sd_setImageWithURL:[NSURL URLWithString:[self.galleryAry[indexPath.row] valueForKey:@"original"]] placeholderImage:nil] ;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}
- (void)setGalleryAry:(NSArray *)galleryAry
{
    _galleryAry = galleryAry;
    [_horTable reloadData];
}

@end


@implementation DDCommentGalleryCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.icon = [[UIImageView alloc]initWithFrame:CGRectMake(5, 5, 65, 65)];
        _icon.layer.cornerRadius = 3.0;
        _icon.clipsToBounds = YES;
        _icon.contentMode = UIViewContentModeScaleAspectFill;
        [self.contentView addSubview:_icon];
    }
    return self;
}

@end
